#include "compile_run.hpp"
#include "../comm/httplib.h"

using namespace ns_compile_run;
using namespace httplib;

static void Usage(std::string proc)
{
    std::cout << "Usage:" << "\r\n" << proc << " port" << std::endl;
}

int main(int args,char* argv[])
{
    if(args != 2)
    {
        Usage(argv[0]);
        exit(-1);
    }

    Server svr;

    svr.Post("/compile_and_run",[](const Request& req,Response& resp){
        std::string in_json = req.body;
        std::string out_json;
        if(!in_json.empty())
        {
            Compile_run::Start(in_json,&out_json);
            resp.set_content(out_json,"application/json;charset=utf-8;");
        }
    });

    // svr.Get("/hello",[](const Request &req, Response &resp){
    //     // 用来进行基本测试
    //     resp.set_content("hello httplib,你好 httplib!", "text/plain;charset=utf-8");
    // });

    //svr.set_base_dir("./wwwroot");

    svr.listen("0.0.0.0",atoi(argv[1]));

    //测试(没加网络的版本)
    // // std::string code = "code";
    // // Compiler::compile(code);
    // // Run::run(code,1,10240);

    // Json::Value in_json;

    // in_json["code"] = R"(#include<iostream>
    //  int main(){
    //      std::cout << "你好" << std::endl;
    //      return 0;
    //  })";

    // in_json["input"] = "";
    // in_json["cpu_limit"] = 1;
    // in_json["as_limit"] = 10240*3;
    // Json::StyledWriter writer;
    // std::string in = writer.write(in_json);

    // std::string out_json;
    // Compile_run::Start(in,&out_json);

    // std::cout << out_json << std::endl;

    // return 0;
}
